﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Media;
using System.IO;
using WindowsMedia;

namespace WindowsMedia
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            wmpObject.settings.autoStart = false;
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            string url = string.Empty;

            try
            {
                if (wmpObject.URL == string.Empty && wmpObject.playState == WMPLib.WMPPlayState.wmppsUndefined)
                {
                    url = urlTextBox.Text;
                    setPlayerURL(url);
                    playPlayer();
                    playButton.Text = "Pause";
                }
                else if (wmpObject.URL != string.Empty && wmpObject.playState == WMPLib.WMPPlayState.wmppsPaused)
                {
                    playPlayer();
                    playButton.Text = "Pause";
                }
                else if (wmpObject.URL != string.Empty && wmpObject.playState == WMPLib.WMPPlayState.wmppsStopped)
                {
                    playPlayer();
                    playButton.Text = "Pause";
                }
                else if (wmpObject.URL != string.Empty && wmpObject.playState == WMPLib.WMPPlayState.wmppsPlaying)
                {
                    pausePlayer();
                    playButton.Text = "Play";
                }                
                else
                {
                    MessageBox.Show(wmpObject.playState.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }              

        private void stopButton_Click(object sender, EventArgs e)
        {
            stopPlayer();
            playButton.Text = "Play";
        }

        //-----PLAYER-CONTROLS-----

        private void setPlayerURL(string url)
        {
            try
            {
                wmpObject.URL = url;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        } 

        private void pausePlayer()
        {
            wmpObject.Ctlcontrols.pause();
        }

        private void playPlayer()
        {
            wmpObject.Ctlcontrols.play();
        }

        private void stopPlayer()
        {
            wmpObject.Ctlcontrols.stop();
        }

        //-----PLAYER-EVENTS-----

        private void wmpObject_ErrorEvent(object sender, EventArgs e)
        {
            //MessageBox.Show("need to find out how to get error from event");
        }
    }
}
